import { login, getInfo, getMenus } from '@/api/user'
import { getToken, setToken, removeToken, setUserName, getMenuList, setRoutes, removeCookieKey } from '@/utils/auth'
import { resetRouter } from '@/router'
import { Message } from 'element-ui'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  menus: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USER_NAME: (state, name) => {
    state.name = name
    setUserName(name)
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_MENUS: (state, list) => {
    state.menus = list
    list.length > 0 && setRoutes(list)
  }
}

const actions = {
  // user login
  async login({ commit }, userInfo) {
    const { username, password } = userInfo
    const resp = await login({ username: username.trim(), password: password })
    if (resp.data.state === '000') {
      Message({
        message: resp.data.msg,
        type: 'success'
      })
      const { data } = resp.data
      commit('SET_TOKEN', data.token)
      commit('SET_USER_NAME', data.uname)
      setToken(data.token)
    }
    return resp
    // return new Promise((resolve, reject) => {
    //   login({ username: username.trim(), password: password }).then(response => {
    //     const { data } = response.data
    //     commit('SET_TOKEN', data.token)
    //     setToken(data.token)
    //     resolve()
    //   }).catch(error => {
    //     reject(error)
    //   })
    // })
  },
  async getMenus({ commit }) {
    const resp = await getMenus()
    if (resp.data.state === '000') {
      return new Promise((resolve, reject) => {
        const menus = getMenuList(resp.data.data)
        commit('SET_MENUS', menus)
        window.sessionStorage.setItem('mainRoute', resp.data.data[0].uri)
        resolve({ menus, state: '000' })
      })
    } else {
      return Promise.resolve(resp)
    }
  },
  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          reject('验证失败，请重新登录！')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit('SET_TOKEN', '')
      commit('SET_MENUS', [])
      removeToken()
      resetRouter()
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  },

  removeCookie() {
    return new Promise((resolve, reject) => {
      removeCookieKey('token')
      removeCookieKey('username')
      removeCookieKey('menus')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

